//
//  WSAskViewController.m
//  RunningMan
//
//  Created by 王帅 on 16/5/18.
//  Copyright © 2016年 WSJ. All rights reserved.
//

#import "WSAskViewController.h"
#import "WSAskTabViewCell.h"
#import "WSMyTool.h"
#import "WSSportCirleWeb.h"
@interface WSAskViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSArray *askArray;
@property (nonatomic, assign) NSInteger page;
@end

@implementation WSAskViewController

- (NSArray *)askArray {
    if (!_askArray) {
        _askArray = [NSArray array];
    }
    return _askArray;
}
- (void)viewDidLoad {
    [[UINavigationBar appearance] setTranslucent:NO];
    [super viewDidLoad];
    [self createTableView];
    [self createData];
}
- (void)createTableView {
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 113)];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.rowHeight = 300;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView registerClass:[WSAskTabViewCell class] forCellReuseIdentifier:NSStringFromClass([WSAskTabViewCell class])];
    [self.view addSubview:self.tableView];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.askArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    WSAskTabViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([WSAskTabViewCell class]) forIndexPath:indexPath];
    Infos *model = self.askArray[indexPath.row];
    cell.model = model;
    return cell;
    
}
- (void)getData {
    AFHTTPSessionManager *maneger = [AFHTTPSessionManager manager];
    [maneger GET:@"http://api.fit-time.cn/ftinfo/refreshInfos?cat=2&page_size=20" parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        WSJSportCircleModel *model = [WSJSportCircleModel mj_objectWithKeyValues:responseObject];
        self.askArray = model.infos;
        [self.tableView reloadData];
        [self.tableView.mj_header endRefreshing];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        
    }];
}
- (void)getMoreData {
    AFHTTPSessionManager *maneger = [AFHTTPSessionManager manager];
    [maneger GET:[NSString stringWithFormat:@"http://api.fit-time.cn/ftinfo/refreshInfos?cat=2&page_size=%ld", self.page * 20 + self.page] parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        WSJSportCircleModel *model = [WSJSportCircleModel mj_objectWithKeyValues:responseObject];
        self.askArray = model.infos;
        [self.tableView reloadData];
        [self.tableView.mj_footer endRefreshing];

        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        
    }];
}
- (void)createData {
    self.page = 0;
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self getData];
        
    }];
    [self.tableView.mj_header beginRefreshing];
    
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        self.page = self.page + 1;
        [self getMoreData];
    }];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    WSSportCirleWeb *web = [[WSSportCirleWeb alloc] init];
    web.hidesBottomBarWhenPushed = YES;
    Infos *model = self.askArray[indexPath.row];
    web.url = model.url;
    [self.navigationController pushViewController:web animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
